/***********************************************
 This file is part of redia-bapps-main-ios.
 
 Copyright (c) 2015 Redia A/S
 
 Redia-bapps-main-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-bapps-main-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-bapps-main-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

//
//  BAPatronHoldingsViewController.m
//  redia-bapps-ios
//
//  Created by Henrik Lynbech on 14/08/13.
//  Copyright (c) 2013 redia. All rights reserved.
//

#import "BAPatronHoldingsViewController.h"
#import "BALoginViewController.h"
#import "BARPCManager.h"
#import "BAUtils.h"
#import "BAFlyoutMenuViewController.h"
#import "BALibrarySettings.h"
#import "BAErrorCodes.h"

@interface BAPatronHoldingsViewController () {
    BOOL isCheckingLogin;
    BOOL isFetchingLoans;
    BOOL isFetchingReservations;
    UIAlertView* currentAlertView;
}

@property (weak, nonatomic) IBOutlet UIImageView *leftTabImage;
@property (weak, nonatomic) IBOutlet UIImageView *rightTabImage;
@property (weak, nonatomic) IBOutlet UILabel *rightTabResevationLabel;
@property (weak, nonatomic) IBOutlet UILabel *leftTabDeliverLabel;
@property (weak, nonatomic) IBOutlet UILabel *leftTabDeliverCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *rightTabReservationCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *LeftTabTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *rightTabTitleLabel;
@property (weak, nonatomic) IBOutlet UIView *viewTopCustomerColorLeft;
@property (weak, nonatomic) IBOutlet UIView *viewTopCustomerColorRight;

@property (nonatomic, assign) BOOL userRejectedLogin;

@end

@implementation BAPatronHoldingsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    RD_SET_EDGES_FOR_EXTENDED_LAYOUT_IOS7
    BA_LABEL_SET_FONT_LIGHT(self.rightTabResevationLabel);
    BA_LABEL_SET_FONT_LIGHT(self.leftTabDeliverLabel);
    BA_LABEL_SET_FONT_REGULAR(self.leftTabDeliverCountLabel);
    BA_LABEL_SET_FONT_REGULAR(self.rightTabReservationCountLabel);
    BA_LABEL_SET_FONT_LIGHT(self.LeftTabTitleLabel);
    BA_LABEL_SET_FONT_LIGHT(self.rightTabTitleLabel);
    
    BA_FLYOUT_MENU_ADD_BUTTON(BASpecialNavigationBarConfigurationDefault)
    
    UIButton *refreshButton = [UIButton buttonWithType:UIButtonTypeCustom]; \
    [refreshButton setImage:[UIImage imageNamed:@"refresh_grey"] forState:UIControlStateNormal]; \
    [refreshButton addTarget:self action:@selector(refreshListsFromServerIfManually) forControlEvents:UIControlEventTouchUpInside]; \
    [refreshButton setFrame:CGRectMake(0, 0, 32, 32)];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    
    item.width = ([[BAUtils instance] iOSMajorVersion] > 6) ? -6 : 5;
    
    [self.navigationItem setLeftBarButtonItems:@[item, [[UIBarButtonItem alloc] initWithCustomView:refreshButton]]];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(receiveAddReservationNotification:)
                                                 name:BA_RESERVATION_NEW_RESERVATION_ADDED
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(receiveDeleteReservationNotification:)
                                                 name:BA_RESERVATION_RESERVATION_DELETED
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(receivedDidBecomeActiveNotification:)
                                                 name:BA_APP_DELEGATE_DID_BECOME_ACTIVE
                                               object:nil];
    
    
    UITapGestureRecognizer* tapLeft = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapSwitch:)];
    UITapGestureRecognizer* tapRight = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapSwitch:)];
    [self.leftTabImage addGestureRecognizer:tapLeft];
    [self.rightTabImage addGestureRecognizer:tapRight];
    [self.rightTabResevationLabel setAlpha:.6f];
    [self.rightTabTitleLabel setAlpha:.6f];
    [self.rightTabReservationCountLabel setAlpha:.6f];

    //moved to viewDidAppear because of iOS 8 ipad rotation issue
//    [self showLoans];
    
    [self setupCustomerColors:self];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setupCustomerColors:) name:BA_LIBRARY_SETTINGS_OBJECT_CHANGED object:nil];
    //deprecated: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userRequestedLogout) name:BA_DID_REQUEST_LOGOUT object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userRequestedLogout) name:BA_PATRON_SETTINGS_OBJECT_CHANGED object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loansListNeedsRefreshNow) name:BA_LOANS_LIST_NEEDS_REFRESH_NOW object:nil];

    
}

-(void)loansListNeedsRefreshNow
{
    [self refreshLoansList];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self enableUserInteraction];
    
    if (loansViewController==nil) {
        //this would be the first time this tab is opened and viewed
        [self showLoans];
    }
    
    if (self.userRejectedLogin) {
        self.userRejectedLogin = NO;
    }
    else {
        [self refreshListsFromServerIfNeeded];
    }
}

-(void)setupCustomerColors:(id)sender
{
    self.viewTopCustomerColorLeft.backgroundColor = [[BALibrarySettings instance] getCustomerColorNormal];
    self.viewTopCustomerColorRight.backgroundColor = [[BALibrarySettings instance] getCustomerColorNormal];
}

- (void)userRequestedLogout
{
    [BARPCManager closeConnectionsForView:self];
    loans = nil;
    [self.leftTabDeliverCountLabel setText:@"--"];
    [loansViewController refreshLoansList:[NSMutableArray new]];
    reservations = nil;
    [self.rightTabReservationCountLabel setText:@"--"];
    [reservationsViewController refreshReservationsList:[NSMutableArray new]];
    [BARPCManager instance].reservationsLastUpdated = 0;
}

-(void)dealloc
{
    [BARPCManager closeConnectionsForView:self];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:BA_RESERVATION_NEW_RESERVATION_ADDED object:nil];
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:BA_RESERVATION_RESERVATION_DELETED object:nil];
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:BA_APP_DELEGATE_DID_BECOME_ACTIVE object:nil];
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:BA_LIBRARY_SETTINGS_OBJECT_CHANGED object:nil];
//    //deprecated: [[NSNotificationCenter defaultCenter] removeObserver:self name:BA_DID_REQUEST_LOGOUT object:nil];
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:BA_LOANS_LIST_NEEDS_REFRESH_NOW object:nil];
}

- (void)handleTapSwitch:(UITapGestureRecognizer*)recognizer
{
    UIImageView* view = (UIImageView*)recognizer.view;
    
    if (view == self.leftTabImage) {
        DLog(@"leftTabImage");
        [self showLoans];
        [self.leftTabImage setImage:[UIImage imageNamed:@"left_tab_pressed"]];
        [self.leftTabDeliverCountLabel setAlpha:1.f];
        [self.leftTabDeliverLabel setAlpha:1.f];
        [self.LeftTabTitleLabel setAlpha:1.f];
        [self.rightTabImage setImage:[UIImage imageNamed:@"right_tab"]];
        [self.rightTabResevationLabel setAlpha:.6f];
        [self.rightTabTitleLabel setAlpha:.6f];
        [self.rightTabReservationCountLabel setAlpha:.6f];
    
    } else if(view == self.rightTabImage) {
        DLog(@"RightTabImage");
        [self showReservations];
        [self.leftTabImage setImage:[UIImage imageNamed:@"left_tab"]];
        [self.leftTabDeliverCountLabel setAlpha:.6f];
        [self.leftTabDeliverLabel setAlpha:.6f];
        [self.LeftTabTitleLabel setAlpha:.6f];
        [self.rightTabImage setImage:[UIImage imageNamed:@"right_tab_pressed"]];
        [self.rightTabResevationLabel setAlpha:1.f];
        [self.rightTabTitleLabel setAlpha:1.f];
        [self.rightTabReservationCountLabel setAlpha:1.0f];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)showReservations
{
    if (loansViewController != nil) {
        [loansViewController.view removeFromSuperview];
    }
    if (reservationsViewController == nil) {
        reservationsViewController=[BAPatronReservationsViewController new];
        [self addChildViewController:reservationsViewController];
    }
    [self.contentView addSubview:reservationsViewController.view];
    reservationsViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view]|" options:0 metrics: 0 views:@{@"view":reservationsViewController.view}]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view]|" options:0 metrics: 0 views:@{@"view":reservationsViewController.view}]];
    if (reservations == nil) {
//        [self refreshReservationsList];
        [self refreshListsFromServerIfManually];
    } else {
        [reservationsViewController refreshReservationsList:reservations];
    }
//    if (reservations != nil) {
//        [reservationsViewController refreshReservationsList:reservations];
//    }
    
    //[reservationsViewController refreshReservationsList:reservations];
}

-(void)showLoans
{
    if (reservationsViewController != nil) {
        [reservationsViewController.view removeFromSuperview];

    }
    if (loansViewController == nil) {
        loansViewController=[BAPatronLoansViewController new];
        [self addChildViewController:loansViewController];
    }
    //[loansViewController.view setFrame:self.contentView.bounds];
    [self.contentView addSubview:loansViewController.view];
    
    loansViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view]|" options:0 metrics: 0 views:@{@"view":loansViewController.view}]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view]|" options:0 metrics: 0 views:@{@"view":loansViewController.view}]];
    //[self.contentView layoutIfNeeded];

    if(loans == nil) {
//        [self refreshLoansList];
        [self refreshListsFromServerIfManually];
    } else {
        [loansViewController refreshLoansList:loans];
    }
    
//    if(loans != nil) {
//        [loansViewController refreshLoansList:loans];
//    }
    
//    if (reservations == nil) {
//        [self refreshReservationsList];
//    } else {
//        [reservationsViewController refreshReservationsList:reservations];
//    }
    
//    if (reservations != nil) {
//        [reservationsViewController refreshReservationsList:reservations];
//    }
}

- (void)refreshLoansList
{
    if (isFetchingLoans) {
        return;
    }
    isFetchingLoans=YES;
    
    [BARPCManager getLoansAsyncFromView:self
                            resultBlock:^(NSArray *new_loans) {
                                isFetchingLoans=NO;
                                DLog(@"got %d loans",[new_loans count]);
                                loans = [new_loans mutableCopy];
                                NSString *paddedStr = [NSString stringWithFormat:@"%02d", [new_loans count]];
                                
                                [self.leftTabDeliverCountLabel setText:paddedStr];
                                [loansViewController refreshLoansList:loans];
                            }
                             errorBlock:^(int errorCode, NSString *errorMessage) {
                                 isFetchingLoans=NO;
                                 NSLog(@"ERROR getLoans");
                                 [BARPCManager instance].reservationsLastUpdated = 0;
                                 [loansViewController.loadingIndicator stopAnimating];
                                 loans = nil;
                                 if (!currentAlertView) {
                                     currentAlertView = [[UIAlertView alloc] initWithTitle:@"Fejl" message:[NSString stringWithFormat:@"Kunne ikke hente lån-oversigten. %@", errorMessage] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                                     [currentAlertView show];
                                 }
                             }];
    
//    [BALoginViewController checkLoginStatusFromViewController:self
//                                                  resultBlock:^{
//                                                      [BARPCManager getLoansAsyncFromView:self
//                                                                              resultBlock:^(NSArray *new_loans) {
//                                                                                  isFetchingLoans=NO;
//                                                                                  DLog(@"got %d loans",[new_loans count]);
//                                                                                  loans = [new_loans mutableCopy];
//                                                                                  NSString *paddedStr = [NSString stringWithFormat:@"%02d", [new_loans count]];
//                                                                                  
//                                                                                  [self.leftTabDeliverCountLabel setText:paddedStr];
//                                                                                  [loansViewController refreshLoansList:loans];
//                                                                              }
//                                                                               errorBlock:^(int errorCode, NSString *errorMessage) {
//                                                                                   isFetchingLoans=NO;
//                                                                                   NSLog(@"ERROR getLoans");
//                                                                                   [BARPCManager instance].reservationsLastUpdated = 0;
//                                                                                   [loansViewController.loadingIndicator stopAnimating];
//                                                                                   loans = nil;
//                                                                                   if (!currentAlertView) {
//                                                                                       currentAlertView = [[UIAlertView alloc] initWithTitle:@"Fejl" message:[NSString stringWithFormat:@"Kunne ikke hente lån-oversigten. %@", errorMessage] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
//                                                                                       [currentAlertView show];
//                                                                                   }
//                                                                               }];
//                                                  }
//                                                   errorBlock:^(int errorCode, NSString *errorMessage) {
//                                                       isFetchingLoans=NO;
//                                                       if (errorCode == BA_ERROR_LOGIN_USER_REJECTED) {
//                                                           self.userRejectedLogin = YES;
//                                                       }
//                                                       else {
//                                                           loans = nil;
//                                                           if (!currentAlertView) {
//                                                               currentAlertView = [[UIAlertView alloc] initWithTitle:@"Fejl" message:[NSString stringWithFormat:@"Kunne ikke logge ind for at hente lån-oversigten. %@", errorMessage] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
//                                                               [currentAlertView show];
//                                                           }
//                                                       }
//                                                       [loansViewController.loadingIndicator stopAnimating];
//                                                  }];
}

- (void)refreshReservationsList
{
    if (isFetchingReservations) {
        return;
    }
    isFetchingReservations=YES;
    
    [BARPCManager getReservationsAsyncFromView:self
                                   resultBlock:^(NSArray* result) {
                                       isFetchingReservations=NO;
                                       DLog(@"got %d reservations",  [result count]);
                                       NSArray *sortedArray;
                                       sortedArray = [result sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
                                           int first = [(BAReservation*)a pickUpExpireDate];
                                           int second = [(BAReservation*)b pickUpExpireDate];
                                           return first < second;
                                       }];
                                       NSString *paddedStr = [NSString stringWithFormat:@"%02d", [result count]];
                                       [self.rightTabReservationCountLabel setText:paddedStr];
                                       reservations = [sortedArray mutableCopy];
                                       [reservationsViewController refreshReservationsList:reservations];
                                   }
                                    errorBlock:^(int errorCode, NSString *errorMessage) {
                                        isFetchingReservations=NO;
                                        NSLog(@"ERROR getReservations");
                                        [BARPCManager instance].reservationsLastUpdated = 0;
                                        [reservationsViewController.loadingIndicator stopAnimating];
                                        reservations = nil;
                                        if (!currentAlertView) {
                                            currentAlertView = [[UIAlertView alloc] initWithTitle:@"Fejl" message:[NSString stringWithFormat:@"Kunne ikke hente reserveringsoversigten. %@", errorMessage] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                                            [currentAlertView show];
                                        }
                                    }];
    
//    [BALoginViewController checkLoginStatusFromViewController:self
//                                                  resultBlock:^{
//                                                      [BARPCManager getReservationsAsyncFromView:self
//                                                                                     resultBlock:^(NSArray* result) {
//                                                                                         isFetchingReservations=NO;
//                                                                                         DLog(@"got %d reservations",  [result count]);
//                                                                                         NSArray *sortedArray;
//                                                                                         sortedArray = [result sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
//                                                                                             int first = [(BAReservation*)a pickUpExpireDate];
//                                                                                             int second = [(BAReservation*)b pickUpExpireDate];
//                                                                                             return first < second;
//                                                                                         }];
//                                                                                         NSString *paddedStr = [NSString stringWithFormat:@"%02d", [result count]];
//                                                                                         [self.rightTabReservationCountLabel setText:paddedStr];
//                                                                                         reservations = [sortedArray mutableCopy];
//                                                                                         [reservationsViewController refreshReservationsList:reservations];
//                                                                                     }
//                                                                                      errorBlock:^(int errorCode, NSString *errorMessage) {
//                                                                                          isFetchingReservations=NO;
//                                                                                          NSLog(@"ERROR getReservations");
//                                                                                          [BARPCManager instance].reservationsLastUpdated = 0;
//                                                                                          [reservationsViewController.loadingIndicator stopAnimating];
//                                                                                          reservations = nil;
//                                                                                          if (!currentAlertView) {
//                                                                                              currentAlertView = [[UIAlertView alloc] initWithTitle:@"Fejl" message:[NSString stringWithFormat:@"Kunne ikke hente reserveringsoversigten. %@", errorMessage] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
//                                                                                              [currentAlertView show];
//                                                                                          }
//                                                                                      }];
//                                                  } errorBlock:^(int errorCode, NSString *errorMessage) {
//                                                      isFetchingReservations=NO;
//                                                      reservations = nil;
//                                                      if (errorCode == BA_ERROR_LOGIN_USER_REJECTED) {
//                                                          self.userRejectedLogin = YES;
//                                                      }
//                                                      else {
//                                                          if (!currentAlertView) {
//                                                              currentAlertView = [[UIAlertView alloc] initWithTitle:@"Fejl"
//                                                                                                            message:[NSString stringWithFormat:@"Kunne ikke logge ind for at hente reserveringsoversigten. %@", errorMessage]
//                                                                                                           delegate:self
//                                                                                                  cancelButtonTitle:@"OK"
//                                                                                                  otherButtonTitles:nil];
//                                                              [currentAlertView show];
//                                                          }
//                                                      }
//                                                      [reservationsViewController.loadingIndicator stopAnimating];
//                                                  }];
}

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    currentAlertView=nil;
    DLog(@"clickedButtonAtIndex: %d", buttonIndex);
    //loans = nil;
    //reservations = nil;
}

- (void) receiveAddReservationNotification:(NSNotification *) notification
{
    if ([[notification name] isEqualToString:BA_RESERVATION_NEW_RESERVATION_ADDED]) {
        [self refreshReservationsList];
    }
}

- (void) receiveDeleteReservationNotification:(NSNotification*) notification
{
    if ([[notification name] isEqualToString:BA_RESERVATION_RESERVATION_DELETED]) {
        [self refreshReservationsList];
        NSString *paddedStr = [NSString stringWithFormat:@"%02d", [reservationsViewController.reservations count]];
        [self.rightTabReservationCountLabel setText:paddedStr];
    }
}

- (void) refreshListsFromServerIfNeeded
{
//    if (isFetchingLoans || isFetchingReservations || isCheckingLogin) {
//        return;
//    }
    NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
    DLog(@"Last updated: %f", now - [BARPCManager instance].reservationsLastUpdated);
    if(now - [BARPCManager instance].reservationsLastUpdated > 600) { //Refresh every 10 minutes
        [self refreshListsFromServerIfManually];
        self.userRejectedLogin = NO;
        isCheckingLogin = YES;
        [BALoginViewController checkLoginStatusFromViewController:self
                                                      resultBlock:^{
                                                          isCheckingLogin = NO;
                                                          [loansViewController.loadingIndicator startAnimating];
                                                          [self refreshLoansList];
                                                          [reservationsViewController.loadingIndicator startAnimating];
                                                          [self refreshReservationsList];
                                                      }
                                                       errorBlock:^(int errorCode, NSString *errorMessage) {
                                                           isCheckingLogin = NO;
                                                           if (errorCode == BA_ERROR_LOGIN_USER_REJECTED) {
                                                               self.userRejectedLogin = YES;
                                                           }
                                                           else {
                                                               loans = nil;
                                                               if (!currentAlertView) {
                                                                   currentAlertView = [[UIAlertView alloc] initWithTitle:@"Fejl" message:[NSString stringWithFormat:@"Kunne ikke logge ind for at hente lån-oversigten. %@", errorMessage] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                                                                   [currentAlertView show];
                                                               }
                                                           }
                                                           [loansViewController.loadingIndicator stopAnimating];
                                                       }];
    }
}

- (void) refreshListsFromServerIfManually
{
    if (isFetchingLoans || isFetchingReservations || isCheckingLogin) {
        return;
    }
    self.userRejectedLogin = NO;
    isCheckingLogin = YES;
    [BALoginViewController checkLoginStatusFromViewController:self
                                                  resultBlock:^{
                                                      isCheckingLogin = NO;
                                                      [loansViewController.loadingIndicator startAnimating];
                                                      [self refreshLoansList];
                                                      [reservationsViewController.loadingIndicator startAnimating];
                                                      [self refreshReservationsList];
                                                  }
                                                   errorBlock:^(int errorCode, NSString *errorMessage) {
                                                       isCheckingLogin = NO;
                                                       if (errorCode == BA_ERROR_LOGIN_USER_REJECTED) {
                                                           self.userRejectedLogin = YES;
                                                       }
                                                       else {
                                                           loans = nil;
                                                           if (!currentAlertView) {
                                                               currentAlertView = [[UIAlertView alloc] initWithTitle:@"Fejl" message:[NSString stringWithFormat:@"Kunne ikke logge ind for at hente lån- og reserveringsoversigten. %@", errorMessage] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                                                               [currentAlertView show];
                                                           }
                                                       }
                                                       [loansViewController.loadingIndicator stopAnimating];
                                                       [reservationsViewController.loadingIndicator stopAnimating];
                                                   }];

}



- (void) receivedDidBecomeActiveNotification:(NSNotification*) notification
{
    DLog(@"receivedDidBecomeActiveNotification");
    if(self.tabBarController.selectedViewController == self.navigationController){
        [self refreshListsFromServerIfNeeded];
    }
    
}

#pragma mark - Flyout menu

BA_FLYOUT_MENU_ADD_CALLBACK

- (void)enableUserInteraction
{
    self.view.userInteractionEnabled = YES;
    self.tabBarController.tabBar.userInteractionEnabled = YES;
}

- (void)disableUserInteraction
{
    self.view.userInteractionEnabled = NO;
    self.tabBarController.tabBar.userInteractionEnabled = NO;
}

@end
